/*
 * CS_SOCIAL_REQUEST_MESSAGE_LIST.cpp
 *
 *  Created on: 2013. 5. 27.
 *      Author: the2
 */


#include "../../Command.h"
#include "../../User.h"
#include "../../NResource/Static.h"

namespace ProjectZ { namespace NCommand {
/*
// 카톡 메시지 리스트
CS_SOCIAL_REQUEST_MESSAGE_LIST		0X2112
{
}

SC_SOCIAL_REQUEST_MESSAGE_LIST		0X2113
{
	s16 s16ack;
	u8 u8count;
	count{
		u8 u8type;
		str strMessage;
	}
}
*/
void Command::CS_SOCIAL_REQUEST_MESSAGE_LIST(User * pUser, ::Network::Packet * pPacket)
{
	PREPARE_PACKET;

	NResource::MessageData * pMessage = NResource::Static::InstancePtr()->GetMessageResource()->GetMessageData(pUser);
	if (NULL == pMessage)
	{
		PROCESSLOG("CS_SOCIAL_REQUEST_MESSAGE_LIST : pMessage is NULL!!");
		return;
	}

	time_t curTime = time(NULL);

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ACK_OK);
	*pAck << static_cast<uint8_t>(pMessage->GetKakaoMessageCount());

	NResource::MessageData::DATATREE & msgTree = pMessage->GetTree();

	auto iter = msgTree.begin();
	for (; iter != msgTree.end(); ++iter)
	{
		if (false == pMessage->IsKakaoMessage(iter->first))
			continue;

		*pAck << static_cast<uint8_t>(iter->first);

		if ( (iter->second->_S_TIME1 <= curTime && curTime <= iter->second->_E_TIME1)
				|| (iter->second->_S_TIME2 <= curTime && curTime <= iter->second->_E_TIME2)
				|| (iter->second->_S_TIME3 <= curTime && curTime <= iter->second->_E_TIME3) )
		{
			int value = rand() % 3;

			switch (value)
			{
			case 0: *pAck << iter->second->_MSG1.c_str(); continue;
			case 1: *pAck << iter->second->_MSG2.c_str(); continue;
			case 2: *pAck << iter->second->_MSG3.c_str(); continue;
			}
		}

		*pAck << iter->second->_MSG0.c_str();
	}

	pAck->MakePacket(CMD_SC_SOCIAL_REQUEST_MESSAGE_LIST);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/
